package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.SetmealCheckgroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.SetmealCheckgroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper,CheckGroup> implements CheckGroupService {

    @Autowired
    private CheckGroupMapper mapper;

    @Autowired
    private SetmealCheckgroupMapper setmealCheckgroupMapper;

    //查询检查组（分页查询）
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page<CheckGroup> cgmp = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        LambdaQueryWrapper<CheckGroup> wrapper = new LambdaQueryWrapper();
        if (null!=queryPageBean.getQueryString()){
            wrapper.eq(CheckGroup::getCode,queryPageBean.getQueryString())
                    .or().like(CheckGroup::getName,queryPageBean.getQueryString())
                    .or().eq(CheckGroup::getHelpCode,queryPageBean.getQueryString());
        }
        Page<CheckGroup> checkGroupPage = mapper.selectPage(cgmp, wrapper);

        List<CheckGroup> records = checkGroupPage.getRecords();
        for (CheckGroup record : records) {
            List<CheckItem> checkItems = mapper.findByGroupId(record);
            record.setCheckItems(checkItems);
        }
        return new PageResult(checkGroupPage.getTotal(),checkGroupPage.getRecords());
    }

    //新增检查组（多表添加）
    @Override
    public Result add(Integer id,Integer[] checkitemIds) {
        if (checkitemIds == null){
            return new Result(false,"参数异常");
        }
        for (Integer checkitemId : checkitemIds) {
            mapper.add(id,checkitemId);
        }
        return new Result(true,"新增检查组成功");
    }
    //检查项数据回显
    @Override
    public Result findCheckItemIdsByCheckGroupId(Integer id) {
        Integer[] checkitemIds = mapper.findCheckItemIdsByCheckGroupId(id);
        return new Result(true,"查询检查项成功",checkitemIds);
    }
    //修改多种关联的就先删除后添加
    //删除
    @Override
    public void deleteCheckitemId(Integer id) {
        mapper.deleteCheckitemId(id);
    }
    //套餐检查组查询
    @Override
    public Result findAll() {
        List<CheckGroup> groupList = mapper.selectList(null);
        return new Result(true,"查询检查组成功",groupList);
    }
    //套餐检查组回显
    @Override
    public Result findCheckGroupIdsBySetmealId(Integer id) {
        LambdaQueryWrapper<SetmealCheckgroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealCheckgroup::getSetmealId,id);
        List<SetmealCheckgroup> setmealCheckgroups = setmealCheckgroupMapper.selectList(wrapper);

        List<Integer> list = new ArrayList<>();
        for (SetmealCheckgroup setmealCheckgroup : setmealCheckgroups) {
            list.add(setmealCheckgroup.getCheckgroupId());
        }
        return new Result(true,"检查组回显成功",list);
    }
}
